package cn.wandersnail.ble.ota;


import android.content.Context;
import android.os.Environment;
import android.util.Log;

import java.io.File;

import cn.wandersnail.ble.Connection;

public class OTAController {
    private static final String TAG = "OTAController";
    private Context mContext;
    private OTACallback mCallback;
    private OTACore mOTACore;
    private Boolean mOTAFileExists = false;


    public OTAController(Context context){
        mContext = context;
        //initFiles();
        mOTACore = new OTACore(mContext, null);
    }

    public void setOTACallback(OTACallback callback){
        mCallback = callback;
        mOTACore.setOTACallback(mCallback);
    }

    public void destroy(){
        mOTACore = null;
    }

    public void startOTA(String binFile){
        mOTACore.startDownload(binFile);
    }

    public void setConnection(Connection connection){
        mOTACore.setConnection(connection);
    }

    public boolean hasOTAfile(){
        return mOTAFileExists;
    }

    private void initFiles(){
        Log.i(TAG,"initFiles");
        File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/download/holoswim");
        if(dir.exists() == false){
            dir.mkdir();
        }else{
            Log.i(TAG,"initFiles dir ok");
        }
        File binfile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/download/update.bin");
        if(binfile.exists()){
            mOTAFileExists = true;
        }
    }
}
